import type { IMMessage } from '@xkit-yx/im-store'
import { t } from './i18n'
import type { TMsgScene } from 'nim-web-sdk-ng/dist/NIM_MINIAPP_SDK/MsgServiceInterface';
import { setTabbarData } from '@/utils/wx';
const translate = (key : string) : string => {
	const text =
		{
			textMsgText: t('textMsgText'),
			customMsgText: t('customMsgText'),
			audioMsgText: t('audioMsgText'),
			videoMsgText: t('videoMsgText'),
			fileMsgText: t('fileMsgText'),
			callMsgText: t('callMsgText'),
			geoMsgText: t('geoMsgText'),
			imgMsgText: t('imgMsgText'),
			notiMsgText: t('notiMsgText'),
			robotMsgText: t('robotMsgText'),
			tipMsgText: t('tipMsgText'),
			unknowMsgText: t('unknowMsgText'),
		}[key] || ''
	return `[${text}]`
}

export const getMsgContentTipByType = (
	msg : Pick<IMMessage, 'type' | 'body'>
) : string => {
	const { type, body } = msg
	switch (type) {
		case 'text':
			return body || translate('textMsgText')
		case 'file':
			return translate('fileMsgText')
		case 'image':
			return translate('imgMsgText')
		case 'custom':
			return body || translate('customMsgText')
		case 'audio':
			return translate('audioMsgText')
		case 'g2':
			return translate('callMsgText')
		case 'geo':
			return translate('geoMsgText')
		case 'notification':
			return translate('notiMsgText')
		case 'robot':
			return translate('robotMsgText')
		case 'tip':
			return translate('tipMsgText')
		case 'video':
			return translate('videoMsgText')
		default:
			return translate('unknowMsgText')
	}
}

export const setTabUnread = () : void => {
	const unread = uni.$UIKitStore?.uiStore?.sessionUnread || 0;
	setTabbarData({ messageCount: unread });
}

export const setContactTabUnread = () : void => {
	const unread = uni.$UIKitStore?.sysMsgStore?.unreadSysMsgCount;
	console.log('unread', unread);
}

export const parseSessionId = (
	sessionId : string
) : { scene : TMsgScene; to : string } => {
	const [scene, ...to] = sessionId.split('-')
	return {
		scene: scene as TMsgScene,
		// 这样处理是为了防止有些用户 accid 中自带 -
		to: to.join('-'),
	}
}